package com.zbbt.sportslottery.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;

public class LotteryShopMapActivity extends SimpleBaseActivity {
	private String coordinate;// 经纬度
	private String address;// 商店地址，现在服务器暂时没有数据
	private LinearLayout requestLocButton;// 放大缩小按钮布局
	private ImageView btnAmplify;
	private ImageView btnShrink;

	private MapView mMapView;
	private BaiduMap mBaiduMap;
	private boolean isFirstLoc = true;// 是否首次定位
	private double latitude;// 定位获取经度
	private double longitude;// 定位获取纬度
	private LatLng loctionLat;// 定位经纬度
	private float zoomToNum = 14;
	private int radiusnum = 250;
	private LocationClient mLocClient;
	private BitmapDescriptor mCurrentMarker;
	public MyLocationListenner myListener = new MyLocationListenner();

	// 初始化全局 bitmap 信息，不用时及时 recycle
	private BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.drawable.tzzd_bigtag);
	private BitmapDescriptor bdB = BitmapDescriptorFactory.fromResource(R.drawable.tzzd_smalltag);
	private LocationMode mCurrentMode;
	private GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
	private boolean isIntLoc = false;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_showmap);
		Intent intent = getIntent();
		coordinate = intent.getStringExtra("coordinate");
		address = intent.getStringExtra("address");
	}

	@Override
	protected void initView() {
			requestLocButton = (LinearLayout) findViewById(R.id.location_btn);
			btnAmplify = (ImageView) findViewById(R.id.btn3);
			btnShrink = (ImageView) findViewById(R.id.btn4);
			mMapView = (MapView) findViewById(R.id.bmapView);
			mBaiduMap = mMapView.getMap();
			mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15));

			// 初始化搜索模块，注册事件监听
			mSearch = GeoCoder.newInstance();
			mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {

				@Override
				public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
					if (arg0 == null || arg0.error != SearchResult.ERRORNO.NO_ERROR) {
						Toast.makeText(LotteryShopMapActivity.this, "抱歉，未能找到结果", Toast.LENGTH_LONG).show();
						return;
					}
					mBaiduMap.clear();
					mBaiduMap.addOverlay(new MarkerOptions().position(arg0.getLocation()).icon(BitmapDescriptorFactory.fromResource(R.drawable.tzzd_bigtag)).zIndex(0).draggable(true));
					mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(arg0.getLocation()));
					Toast.makeText(LotteryShopMapActivity.this, arg0.getAddress(), Toast.LENGTH_LONG).show();
				}

				@Override
				public void onGetGeoCodeResult(GeoCodeResult arg0) {
					// TODO Auto-generated method stub

				}
			});
			initOverlay();
			// 隐藏缩放控件
			int childCount = mMapView.getChildCount();
			View zoom = null;
			for (int i = 0; i < childCount; i++) {
				View child = mMapView.getChildAt(i);
				if (child instanceof ZoomControls) {
					zoom = child;
					break;
				}
			}
			zoom.setVisibility(View.GONE);
			// 隐藏指南针
			UiSettings mUiSettings = mBaiduMap.getUiSettings();
			mUiSettings.setCompassEnabled(true);
			// 删除百度地图logo
			mMapView.removeViewAt(1);
	}

	public void initLoc() {
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		// 初始化坐标
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(1000);
		mLocClient.setLocOption(option);
		mLocClient.start();
		isIntLoc = true;
	}

	@Override
	protected void setListener() {
		btnAmplify.setOnClickListener(this);
		btnShrink.setOnClickListener(this);
		requestLocButton.setOnClickListener(this);

	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.location_btn:
			initLoc();
			zoomToNum = 14;
			mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(zoomToNum));
			mCurrentMode = LocationMode.FOLLOWING;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
			mCurrentMode = LocationMode.NORMAL;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
			isFirstLoc = true;
			break;
		case R.id.btn3:
			if (zoomToNum < 17) {
				zoomToNum = zoomToNum + 1;
				mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(zoomToNum));
			}
			if (radiusnum != 250) {
				radiusnum = radiusnum - 500;
			}
			if (isIntLoc) {
				mCurrentMode = LocationMode.FOLLOWING;
				mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
				mCurrentMode = LocationMode.NORMAL;
				mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
				isFirstLoc = true;
			} else {
				String coordinateStr[] = coordinate.split(",");
				LatLng ptCenter = new LatLng(Float.valueOf(coordinateStr[0]), (Float.valueOf(coordinateStr[1])));
				MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ptCenter, zoomToNum);
				mBaiduMap.setMapStatus(update);
			}
			break;
		case R.id.btn4:
			zoomToNum = zoomToNum - 1;
			radiusnum = radiusnum + 500;
			if (isIntLoc) {
				mCurrentMode = LocationMode.FOLLOWING;
				mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
				mCurrentMode = LocationMode.NORMAL;
				mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
				isFirstLoc = true;
			} else {
				String coordinateStr[] = coordinate.split(",");
				LatLng ptCenter = new LatLng(Float.valueOf(coordinateStr[0]), (Float.valueOf(coordinateStr[1])));
				MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ptCenter, zoomToNum);
				mBaiduMap.setMapStatus(update);
			}
			break;

		default:
			break;
		}
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	/**
	 * 
	 * @ClassName: MyLocationListenner
	 * @Description: 经纬度监听事件
	 * @author：gaomingshuo
	 * @date：2015-1-23 下午3:34:12
	 * @version V1.0
	 */
	class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null || mMapView == null)
				return;
			if (isFirstLoc) {
				mMapView.getMap().clear();
				isFirstLoc = false;
				// 此处设置开发者获取到的方向信息，顺时针0-360
				MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius()).direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
				mBaiduMap.setMyLocationData(locData);
				latitude = location.getLatitude();
				longitude = location.getLongitude();
				loctionLat = new LatLng(latitude, longitude);
				MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(loctionLat, zoomToNum);
				mBaiduMap.setMapStatus(update);
				String coordinateStr[] = coordinate.split(",");
				LatLng ptCenter = new LatLng(Float.valueOf(coordinateStr[0]), (Float.valueOf(coordinateStr[1])));
				OverlayOptions options = new MarkerOptions().position(ptCenter).icon(bdA).zIndex(0).draggable(true);
				mBaiduMap.addOverlay(options);
			}
		}
	}

	/**
	 * 
	 * @MethodName:initOverlay
	 * @Description: 获取定期后显示标记
	 * @author：gaomingshuo
	 * @date：2015-1-23 下午3:56:47 void
	 */
	public void initOverlay() {
		String coordinateStr[] = coordinate.split(",");
		LatLng ptCenter = new LatLng(Float.valueOf(coordinateStr[0]), (Float.valueOf(coordinateStr[1])));
		// OverlayOptions options = new
		// MarkerOptions().position(ptCenter).icon(bdA).zIndex(0).draggable(true);
		// mBaiduMap.addOverlay(options);

		// 反Geo搜索
		mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));
	}

	@Override
	public void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	public void onDestroy() {
		// 退出时销毁定位
		if (isIntLoc) {
			mLocClient.stop();
		}
		// 关闭定位图层
		mBaiduMap.setMyLocationEnabled(false);
		mMapView.onDestroy();
		mMapView = null;
		mSearch.destroy();
		super.onDestroy();
	}
}
